package com.tc.vms.consumer;

import com.tc.vms.VMSClient;
import com.tc.vms.iface.VmsMetaData;
import com.tc.vms.utils.Utils;
import com.tc.vms.utils.VmsDomainAttr;
import com.tc.vms.utils.ZkUtils;

import java.util.*;

/**
 * Created by yonghua.zhang on 2015/11/10.
 */
public class QueueMetaData extends VmsMetaData {

    protected final String queueName; //user group name
    protected final String realQueueNmOnBroker;
    protected final String consumerInstName;  // for tracing issues
    private final Map<String, Set<String>> chlAndRkey = new HashMap<String, Set<String>>();

    public QueueMetaData(VmsDomainAttr vmsDomainAttr, String queueName, String clientId, Properties props) {
        super(vmsDomainAttr, props, clientId);
        this.queueName = queueName;
        this.realQueueNmOnBroker = ZkUtils.getVmsQueueName(cfgCenterProps);
        this.consumerInstName = Utils.normalize(Utils.mkString("-", VMSClient.getVmsVersion(), "queue", belongAttr.getBelongDomain(),
                belongAttr.getBelongAppname(), Utils.getHostIp(), String.valueOf(Utils.getPid()), origClientId));
        parseBindings(ZkUtils.getVmsQueueBindings(props));
    }

    public boolean isDlqQueueMetaData() {
        return false;
    }

    public String getRealQueueNmOnBroker() {
        return realQueueNmOnBroker;
    }

    public String getRetryQueueName() {
        return RETRY_QUEUE_PREFIX + realQueueNmOnBroker;
    }

    public String getDLQName() {
        return DEAD_LETTER_QUEUE_PREFIX + realQueueNmOnBroker;
    }

    public String getQueueName() {
        return queueName;
    }

    public Set<String> getQueueShards() {
        return Utils.splitShardsTagsByString(ZkUtils.getQueueShards(cfgCenterProps));
    }

    public Map<String, Set<String>> getChlAndRkey() {
        return chlAndRkey;
    }

    public String getConsumerInstName() {
        return consumerInstName;
    }

    private void parseBindings(String bindings) {
        if (bindings != null) {
            String[] items = bindings.split("(\\s+|,)");
            for (String binding : items) {
                if (binding != null && !binding.isEmpty()) {
                    int pos = binding.indexOf('.');
                    if (pos > 0) {
                        String chlName = binding.substring(0, pos);
                        String rKey = binding.substring(pos + 1);

                        if (!chlAndRkey.containsKey(chlName)) {
                            chlAndRkey.put(chlName, new HashSet<String>(2));
                        }
                        Set<String> rKeySet = chlAndRkey.get(chlName);
                        rKeySet.add(rKey);
                    }
                }
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        QueueMetaData that = (QueueMetaData) o;

        if (!queueName.equals(that.queueName)) return false;
        return consumerInstName.equals(that.consumerInstName);

    }

    @Override
    public int hashCode() {
        int result = queueName.hashCode();
        result = 31 * result + consumerInstName.hashCode();
        return result;
    }

    @Override
    public String getZnodeName() {
        return queueName;
    }

    @Override
    public String getClientInstName() {
        return this.consumerInstName;
    }

    @Override
    public String toString() {
        return "QueueMetaData{" +
                "queueName='" + queueName + '\'' +
                ", realQueueNmOnBroker='" + realQueueNmOnBroker + '\'' +
                ", consumerInstName='" + consumerInstName + '\'' +
                ", chlAndRkey=" + chlAndRkey +
                '}';
    }
}
